<!-- 需求列表  -->
<template>
  <pageHeader title="工序列表" :breadCrumb="['工厂建模', '工序设置']" />
  <div class="search-box">
    <a-space>
      <a-input
        v-model="searchForm.keyWord"
        :style="{ width: '220px' }"
        placeholder="工序编码/工序名称"
        @change="Search"
      />
      <DictionaryChoice
        v-model:value="searchForm.processType"
        ref="refProcessType"
        @change="Search"
        code="工序类别"
      />
      <DictionaryChoice
        v-model:value="searchForm.operateType"
        ref="refOperateType"
        @change="Search"
        code="工作方式"
      />
      <a-button type="outline" @click="resetting">
        <template #icon> <icon-refresh /> </template>重置
      </a-button>
    </a-space>
    <a-space> </a-space>
    <a-space>
      <a-button type="primary" @click="addDemand">
        <template #icon>
          <icon-plus />
        </template>
        新增工序
      </a-button>
      <HeaderSettingCopy
        v-model:headerValues="headerValues"
        :defineHeaders="workSerialHerders.herders"
        :type="workSerialHerders.type"
      />
    </a-space>
  </div>
  <tableTurn style="padding: 0 10px" :tableData="getDemandAll" ref="tableRef">
    <a-table-column :width="70" align="center">
      <template #title> 序号 </template>
      <template #cell="{ rowIndex }">
        {{ rowIndex + 1 }}
      </template>
    </a-table-column>
    <a-table-column
      v-for="header in headerValues"
      :key="header.name"
      :width="header.width || 200"
      :title="header.name"
      :fixed="header.fixed"
      :data-index="header.code"
      align="center"
    >
      <template #cell="{ record }" v-if="!header.code">
        <template v-if="header.name === '是否报工'">
          <a-typography-text type="success" v-if="record.isReportingWork === 1">
            是
          </a-typography-text>
          <a-typography-text type="danger" v-if="record.isReportingWork === 2">
            否
          </a-typography-text>
        </template>
        <template v-if="header.name === '状态'">
          <a-typography-text type="success" v-if="record.state === 1">
            启用
          </a-typography-text>
          <a-typography-text type="danger" v-if="record.state === 2">
            停用
          </a-typography-text>
        </template>
        <template v-if="header.name === '工作方式'">
          {{
            refOperateType?.option.find(
              ({ id }: any) => id === record.operateType
            )?.name
          }}
        </template>
        <template v-if="header.name === '工序类别'">
          {{
            refProcessType?.option.find(
              ({ id }: any) => id === record.processType
            )?.name
          }}
        </template>
      </template>
    </a-table-column>

    <a-table-column :width="100" fixed="right" title="操作" align="center">
      <template #cell="{ record }">
        <!-- <a-space>
          <a-link @click="edit(record.id)">修改</a-link>
          <a-popconfirm
            content="是否删除该数据?"
            type="error"
            @ok="del(record.id)"
          >
            <a-link status="danger"> 删除 </a-link>
          </a-popconfirm>
        </a-space> -->

        <a-tooltip content="修改">
          <IconFont
            type="icon-edit"
            :size="26"
            :style="{
              marginRight: '10px',
              cursor: 'pointer',
            }"
            @click="edit(record.id)"
          />
        </a-tooltip>
        <a-tooltip content="删除">
          <a-popconfirm
            content="是否删除该数据?"
            type="error"
            @ok="del(record.id)"
          >
            <IconFont
              type="icon-delete"
              :size="26"
              :style="{
                marginRight: '10px',
                cursor: 'pointer',
              }"
            />
          </a-popconfirm>
        </a-tooltip>
      </template>
    </a-table-column>
  </tableTurn>
  <WorkSerialModal ref="refWorkSerialModal" @refresh="Search" />
</template>

<script lang="ts" setup>
  import { reactive, onMounted, ref, nextTick } from 'vue';
  import { useRouter } from 'vue-router';
  import {
    GetWorkSerialGroupPage,
    DelWorkSerialGroup,
  } from '@/api/FactoryModeling';
  import { workSerialHerders } from '@/utils/tableHerders';
  import WorkSerialModal from './WorkSerialModal.vue';
  // 表格配置
  const args = reactive({
    stripe: false,
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
      onlyCurrent: false,
    },
    bordered: {
      headerCell: true,
    },
    rowKey: 'id',
  });
  let refProcessType = ref();
  let refOperateType = ref();
  const tableRef = ref();
  let searchForm = reactive<any>({});
  // 获取需求列表
  const getDemandAll = async (page: any) => {
    try {
      let res = await GetWorkSerialGroupPage({
        ...page,
        ...searchForm,
        type: 1,
      });
      return Promise.resolve(res);
    } catch (err: unknown) {
      console.log(err);
    }
  };
  let refWorkSerialModal = ref();
  const addDemand = () => {
    refWorkSerialModal.value.loadData();
  };

  const Search = () => {
    tableRef.value.loadData();
  };
  // 编辑
  const edit = (id: number, disabled?: boolean) => {
    refWorkSerialModal.value.loadData(id, disabled);
  };

  const resetting = () => {
    for (const key in searchForm) {
      delete searchForm[key];
    }
    tableRef.value.loadData();
  };
  const del = async (id: number) => {
    await DelWorkSerialGroup({ id });
    tableRef.value.loadData();
  };

  let headerValues = ref<any[]>([]);
</script>

<style scoped lang="less">
  .search-box {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    padding: 10px;
    margin-top: -7px;
  }

  .use-state {
    display: flex;
    justify-content: center;
    text-align: center;
    .cycle {
      position: relative;
      width: 16px;
      height: 16px;
      border-radius: 50%;
      content: '';
      display: inline-block;
      top: 3px;
      right: 10px;
    }
  }

  .greyColor {
    color: #c9cdd4;
  }
  .greyColor-cycle {
    background-color: #c9cdd4;
  }

  // 审核中、执行中
  .blueColor {
    color: #156dff;
  }
  .blueColor-cycle {
    background-color: #156dff;
  }

  // 已通过
  .greenColor {
    color: #31c273;
  }
  .greenColor-cycle {
    background-color: #31c273;
  }

  // 不通过
  .redColor {
    color: #f53f3f;
  }
  .redColor-cycle {
    background-color: #f53f3f;
  }
</style>
